
import os
import webapp2
import cgi
import logging
import urllib
import jinja2
import time

from django.utils import simplejson
from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers


from models.beer import Beer
from models.kegdroid import KegDroid
from models.drinker import Drinker
from models.pour import Pour

 
def timestamp():
   now = time.time()
   localtime = time.localtime(now)
   return time.strftime('%Y%m%d%H%M%S', localtime)


################
class PourHandler(webapp2.RequestHandler):

    def post(self):

        ## POURS
        kegdroid_name = cgi.escape(self.request.get('kegdroid_name'))
        kegdroid_android_id = cgi.escape(self.request.get('android_id'))
        logging.info("android_id")
        logging.info(kegdroid_android_id)

        drinker_gplus_id = cgi.escape(self.request.get('gplus_id'))
        beer_id = cgi.escape(self.request.get('beer_id'))
        kegdroid_tap_number = cgi.escape(self.request.get('tap_number'))
        pour_volume = cgi.escape(self.request.get('volume_poured'))
        kegdroid_remaining_volume = cgi.escape(self.request.get('remaining_keg_volume'))

        # RECORD POUR

        pr = Pour()
        pr.kegdroid_name = kegdroid_name
        pr.kegdroid_android_id = kegdroid_android_id
        pr.drinker_gplus_id = drinker_gplus_id
        pr.beer_id = int(beer_id)
        pr.kegdroid_tap_number = int(kegdroid_tap_number)
        pr.pour_volume = float(pour_volume)
        pr.kegdroid_remaining_volume = float(kegdroid_remaining_volume)
        pr.put()

        #UPDATE KEGDROIDS
        gKId = 19001
        gkd = KegDroid.get_by_id(gKId)        
        kd = KegDroid.all().filter('android_id = ', kegdroid_android_id).get()
        if (not(kd == None)):
            if (beer_id != ''):
                kd.last_beer = int(beer_id)
                gkd.last_beer = int(beer_id)
                
            if (kegdroid_tap_number == '0'):
                kd.volume_remaining_tap_zero = float(kegdroid_remaining_volume)
            if (kegdroid_tap_number == '1'):
                kd.volume_remaining_tap_one = float(kegdroid_remaining_volume)

            if (kd.number_drinks_poured):
                kd.number_drinks_poured = int(kd.number_drinks_poured) + 1
            else:
                kd.number_drinks_poured = 1
            if (gkd.number_drinks_poured):
                gkd.number_drinks_poured = int(gkd.number_drinks_poured) + 1
            else:
                gkd.number_drinks_poured = 1
            if  (kd.total_volume_poured):  
                kd.total_volume_poured = float(kd.total_volume_poured) + float(pour_volume)
            else:
                kd.total_volume_poured = float(pour_volume)

            kd.last_drinker = drinker_gplus_id

            if  (gkd.total_volume_poured):  
                gkd.total_volume_poured = float(gkd.total_volume_poured) + float(pour_volume)
            else:
                gkd.total_volume_poured = float(pour_volume)

            gkd.last_drinker = drinker_gplus_id
            kd.put()
            gkd.put()


    def get(self):
        tag = self.request.get('id')
        if (tag):
            if (tag == "all"):
                output = []
                pours = Pour.all()
                for pr in pours:
                    output.append(pr.to_dict())
                self.response.out.write(simplejson.dumps({'pours': output}).replace("\/", "/"))
        else :
            output = []
            pours = Pour.all()
            for pr in pours:
                output.append(pr.to_dict())
            self.response.out.write(simplejson.dumps({'pours': output}).replace("\/", "/"))

app = webapp2.WSGIApplication([
    ('/pours', PourHandler),
    ('/pours/add', PourHandler)

   
], debug=True)
